#pragma once

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <sys/types.h>
#include <sys/socket.h>

#include <jsoncpp/json/json.h>
#include "Util.hpp"

using namespace std;
// #define MYSELF 1

namespace protocol_ns
{
#define SEP " "
#define SEP_LEN strlen(SEP)
#define HEADER_SEP "\r\n"
#define HEADER_SEP_LEN strlen("\r\n")

    string AddHeader(const string &str)
    {
        std::cout << "AddHeader 之前:\n"
                  << str << std::endl;
        string s = to_string(str.size());
        s += HEADER_SEP;
        s += str;
        s += HEADER_SEP;
        std::cout << "AddHeader 之后:\n"
                  << s << std::endl;
        return s;
    }

    string RemoveHeader(const string &str, int len)
    {
        std::cout << "RemoveHeader 之前:\n"
                  << str << std::endl;
        string res = str.substr(str.size() - HEADER_SEP_LEN - len, len);

        std::cout << "RemoveHeader 之后:\n"
                  << res << std::endl;
        return res;
    }

    int ReadPackage(int sock, string &inbuffer, string *package)
    {
        std::cout << "ReadPackage inbuffer 之前:\n"
                  << inbuffer << std::endl;
        char buffer[1024];
        ssize_t s = recv(sock, buffer, sizeof(buffer - 1), 0);
        if (s <= 0)
            return -1;
        buffer[s] = 0;

        inbuffer += buffer;
        std::cout << "ReadPackage inbuffer 之中:\n"
                  << inbuffer << std::endl;

        auto pos = inbuffer.find(HEADER_SEP);
        if (pos == string::npos)
            return 0;
        string lenStr = inbuffer.substr(0, pos);
        int len = Util::toInt(lenStr);
        int targetPackageLen = lenStr.size() + len + 2 * HEADER_SEP_LEN;
        if (inbuffer.size() < targetPackageLen)
            return 0;

        *package = inbuffer.substr(0, targetPackageLen);
        std::cout << "ReadPackage inbuffer 之后:\n"
                  << inbuffer << std::endl;
        inbuffer.erase(0, targetPackageLen); 
        return len;
    }
    struct Request
    {
        int _x;
        int _y;
        char _op;

        Request() {}
        Request(int x, int y, char op) : _x(x), _y(y), _op(op)
        {
        }
        bool Serialize(std::string *outStr)
        {
            *outStr = "";
#ifdef MYSELF
            std::string x_string = std::to_string(_x);
            std::string y_string = std::to_string(_y);

            *outStr = x_string + SEP + _op + SEP + y_string;
#else
            Json::Value root;
            root["x"] = _x;
            root["y"] = _y;
            root["op"] = _op;

            Json::FastWriter writer;
            *outStr = writer.write(root);
#endif
            return true;
        }

        bool Deserialize(const std::string &inStr)
        {
#ifdef MYSELF
            std::vector<string> result;
            Util::StringSplit(inStr, SEP, &result);
            if (result.size() != 3)
                return false;
            if (result[1].size() != 1)
                return false;

            _x = Util::toInt(result[0]);
            _op = result[1][0];
            _y = Util::toInt(result[2]);
#else
            Json::Value root;
            Json::Reader reader;
            reader.parse(inStr, root);

            _x = root["x"].asInt();
            _y = root["y"].asInt();
            _op = root["op"].asInt();

#endif
        }
    };
    struct Response
    {
        int _result;
        int _code;

        Response() {}
        Response(int result, int code) : _result(result), _code(code) {}

        bool Serialize(string *outStr)
        {
            *outStr = "";
#ifdef MYSELF
            string res_string = to_string(_result);
            string code_string = to_string(_code);

            *outStr = res_string + SEP + code_string;
#else
            Json::Value root;
            root["result"] = _result;
            root["code"] = _code;

            Json::StyledWriter writer;
            *outStr = writer.write(root);
#endif
            return true;
        }
        bool Deserialize(const string inStr)
        {
#ifdef MYSELF
            vector<string> result;
            Util::StringSplit(inStr, SEP, &result);
            if (result.size() != 2)
                return false;

            _result = Util::toInt(result[0]);
            _code = Util::toInt(result[1]);
#else
            Json::Value root;
            Json::Reader reader;
            reader.parse(inStr, root);
            _result = root["result"].asInt();
            _code = root["code"].asInt();
#endif
            return true;
        }
    };
}